#include <iostream>
#include <stack>

using namespace std;

void merge(int *a, long long &ops, int p, int q, int s, int t)
{
	int k = p, l = s;
	int *temp = new int[q - p + 1 + t - s + 1];
	int r = 0;
	while (k <= q && l <= t) {
		if (a[k] <= a[l]) {
			temp[r] = a[k];
			++k;
		} else {
			temp[r] = a[l];
			++l;
			ops += (q - k + 1);
		}
		++r;
	}

	if (k >	q) {
		while (l <= t) {
			temp[r] = a[l];
			++r;
			++l;
		}
	}

	if (l > t) {
		while (k <= q) {
			temp[r] = a[k];
			++r;
			++k;
		}
	}

	for (int i = 0; i < r; i++) {
		a[i + p] = temp[i];
	}

	delete[] temp;
}


void mergeSort(int*a, long long &ops, int low, int high) 
{
	if (low >= high)
		return;

	int length = high - low + 1;
	int subLength = 1;
	while (subLength < length) {
		int p = 0;
		int q = p + subLength - 1;
		int s = q + 1;
		int t = s + subLength - 1 > high ? high : s + subLength - 1;
		while (s <= high) {
			merge(a, ops, p, q, s, t);
			p = t + 1;
			q = p + subLength - 1;
			s = q + 1;
			t = s + subLength - 1 > high ? high : s + subLength - 1;
		}
		subLength *= 2;
	}
}


int split(int *a, int *result, int low, int high)
{
	if (low >= high)
		return -1;
	int swapIndex = low + 1;
	int i = low + 1;
	int temp = 0;
	for (; i <= high; i++) {
		if (a[i] <= a[low]) {
			temp = a[swapIndex];
			a[swapIndex] = a[i];
			a[i] = temp;
			
			temp = result[i];
			result[i] = result[swapIndex];
			result[swapIndex] = temp;
			
			++swapIndex;
		}
	}

	--swapIndex;
	temp = a[swapIndex];
	a[swapIndex] = a[low];
	a[low] = temp;

	temp = result[swapIndex];
	result[swapIndex] = result[low];
	result[low] = temp;

	return swapIndex;
}

void quickSort(int *a, int *result, int low, int high)
{
	typedef pair<int, int> Int_Pair;
	stack<Int_Pair> sequences;
	sequences.push(Int_Pair(low, high));
	while (!sequences.empty()) {
		Int_Pair sequence = sequences.top();
		sequences.pop();
		int splitIndex = split(a, result, sequence.first, sequence.second);
		if (sequence.first < splitIndex - 1) 
			sequences.push(Int_Pair(sequence.first, splitIndex - 1));
		else if (sequence.second > splitIndex + 1)
			sequences.push(Int_Pair(splitIndex + 1, sequence.second));
	}
}

int main(int argc, char * argv[])
{
	int n;
	scanf("%d", &n);
	const int MAX = 500000;
	while (n != 0) {
		int *a = new int[n];
		int *result = new int[n];
		for (int i = 0; i < n; i++) {
			scanf("%d", a + i);
			result[i] = i;
		}

		quickSort(a, result, 0, n - 1);

		
		long long ops = 0, ops1 = 0;
		int positive = 0, negative = 0;
		for (int i = 0; i < n; i++) {
			if ((i - result[i]) < 0) {
				++negative;
				ops1 += (result[i] - i);
			} else if ((i - result[i]) > 0) {
				++positive;
				ops += (i - result[i]);
			}
		}

		ops = ops + ops1 - positive * negative;

		//mergeSort(a, ops, 0, n -1);

		printf("%I64d\n", ops);

		delete[] a;
		delete[] result;
		
		scanf("%d", &n);
	}
	return 0;
}